package edu.ynu.dao;
// default package

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import edu.ynu.po.SaleChance;

/**
 	* A data access object (DAO) providing persistence and search support for SaleChance entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .SaleChance
  * @author MyEclipse Persistence Tools 
 */

public class SaleChanceDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(SaleChanceDAO.class);
		//property constants
	public static final String CUSTOMER_ID = "customerId";
	public static final String SOURCE = "source";
	public static final String TITLE = "title";
	public static final String RATE = "rate";
	public static final String LINKMAN = "linkman";
	public static final String PHONE = "phone";
	public static final String DESCRIPTION = "description";
	public static final String CREATE_BY = "createBy";
	public static final String CREATETIME = "createtime";
	public static final String DUE_TO = "dueTo";
	public static final String DUE_DATE = "dueDate";
	public static final String STAGE = "stage";
	public static final String STATUS = "status";



    
    public void save(SaleChance transientInstance) {
        log.debug("saving SaleChance instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
            session.save(transientInstance);
            trans.commit();
            session.close();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(SaleChance persistentInstance) {
        log.debug("deleting SaleChance instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
        	session.delete(persistentInstance);
            trans.commit();
            session.close();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update(SaleChance persistentInstance) {
        log.debug("deleting SaleChance instance");
        try {
        	Session session = getSession();
        	session.clear();
        	Transaction trans = session.beginTransaction();
        	session.update(persistentInstance);
            trans.commit();
            session.close();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public SaleChance findById( java.lang.Integer id) {
        log.debug("getting SaleChance instance with id: " + id);
        try {
            SaleChance instance = (SaleChance) getSession()
                    .get("edu.ynu.po.SaleChance", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(SaleChance instance) {
        log.debug("finding SaleChance instance by example");
        try {
            List results = getSession()
                    .createCriteria("SaleChance")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding SaleChance instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from SaleChance as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByCustomerId(Object customerId
	) {
		return findByProperty(CUSTOMER_ID, customerId
		);
	}
	
	public List findBySource(Object source
	) {
		return findByProperty(SOURCE, source
		);
	}
	
	public List findByTitle(Object title
	) {
		return findByProperty(TITLE, title
		);
	}
	
	public List findByRate(Object rate
	) {
		return findByProperty(RATE, rate
		);
	}
	
	public List findByLinkman(Object linkman
	) {
		return findByProperty(LINKMAN, linkman
		);
	}
	
	public List findByPhone(Object phone
	) {
		return findByProperty(PHONE, phone
		);
	}
	
	public List findByDescription(Object description
	) {
		return findByProperty(DESCRIPTION, description
		);
	}
	
	public List findByCreateBy(Object createBy
	) {
		return findByProperty(CREATE_BY, createBy
		);
	}
	
	public List findByCreatetime(Object createtime
	) {
		return findByProperty(CREATETIME, createtime
		);
	}
	
	public List findByDueTo(Object dueTo
	) {
		return findByProperty(DUE_TO, dueTo
		);
	}
	
	public List findByDueDate(Object dueDate
	) {
		return findByProperty(DUE_DATE, dueDate
		);
	}
	
	public List findByStage(Object stage
	) {
		return findByProperty(STAGE, stage
		);
	}
	
	public List findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	

	public List findAll(Map paramMap) {
		log.debug("finding all SaleChance instances");
		try {
			String custName = (String)paramMap.get("custName");
			String title = (String)paramMap.get("title");
			String dueTo = (String)paramMap.get("dueTo");
			Criteria c = getSession().createCriteria(SaleChance.class);
			if(StringUtils.isNotEmpty(custName)){
				c.add(Expression.like("customerId", "%"+custName+"%"));
			}
			if(StringUtils.isNotEmpty(title)){
				c.add(Expression.like("title", "%"+title+"%"));
			}
			if(StringUtils.isNotEmpty(dueTo)){
				c.add(Expression.like("dueTo", "%"+dueTo+"%"));
			}
			return c.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public SaleChance merge(SaleChance detachedInstance) {
        log.debug("merging SaleChance instance");
        try {
            SaleChance result = (SaleChance) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(SaleChance instance) {
        log.debug("attaching dirty SaleChance instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(SaleChance instance) {
        log.debug("attaching clean SaleChance instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}